<template>
    <div class="menu">
        <!--        添加/修改菜单-->
        <el-dialog
                class="dialog-center"
                :title="addForm.id?'修改菜单':'新增菜单'"
                :visible.sync="addForm.visible"
                :close-on-click-modal="false"
                width="30%">
            <el-form :model="addForm" :rules="rules" ref="addForm" label-width="100px">
                <el-form-item label="名称" prop="name" style="text-align:left">
                    <el-input v-model="addForm.name" size="mini" style="width:260px;"
                              placeholder="菜单名称"></el-input>
                </el-form-item>

                <el-form-item label="层级" style="text-align:left">
                    <el-input v-model="addForm.level==0?'一级菜单':'二级菜单'" size="mini" style="width:260px;"
                              disabled></el-input>
                </el-form-item>

                <el-form-item label="图标" style="text-align:left">
                    <el-input v-model="addForm.icon" size="mini" style="width:260px;"
                              placeholder="菜单图标"></el-input>
                </el-form-item>

                <el-form-item label="路由" prop="url" style="text-align:left">
                    <el-input v-model="addForm.url" size="mini" style="width:260px;"
                              placeholder="菜单路由,前端项目中的路由"></el-input>
                </el-form-item>

                <el-form-item label="备注" style="text-align:left">
                    <el-input
                            type="textarea"
                            :rows="2"
                            style="width: 60%"
                            v-model="addForm.remark">
                    </el-input>
                </el-form-item>

            </el-form>
            <div align="center">
                <el-button type="primary" @click="submitForm('addForm')" size="small">{{addForm.id?'提交修改':'立即创建'}}
                </el-button>
                <el-button @click="resetForm('addForm')" size="small">重置</el-button>
            </div>

        </el-dialog>
        <el-row type="flex" justify="start">
            <el-col :span="24" style="text-align:left">
                <el-form :inline="true" :model="searchParam" class="demo-form-inline">
                    <el-form-item label="菜单名称">
                        <el-input v-model="searchParam.name" placeholder="菜单名称" size="mini"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="el-icon-search" size="mini" @click="searchMenuList()">查询
                        </el-button>
                        <el-button type="primary" icon="el-icon-add" size="mini" @click="showAddMenuDialog()">添加一级菜单
                        </el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="24">
                <el-table
                        ref="multipleTable"
                        :data="tableData"
                        tooltip-effect="dark"
                        row-key="id"
                        highlight-current-row
                        default-expand-all
                        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
                        style="width: 100%">
                    <el-table-column
                            prop="name"
                            label="名称"
                            show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column
                            label="菜单层级"
                            align="center"
                            show-overflow-tooltip>
                        <template slot-scope="scope">{{scope.row.level==0?'一级菜单':'二级菜单'}}</template>
                    </el-table-column>
                    <el-table-column
                            prop="icon"
                            label="图标"
                            align="center"
                            show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column
                            prop="url"
                            label="路由地址"
                            align="center"
                            show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column
                            prop="remark"
                            label="备注"
                            align="center"
                            show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column label="操作" min-width="150" fixed="right">
                        <template slot-scope="scope">
                            <el-button size="mini" type="warning" @click="deletMenuAndChildren(scope.row)">删除
                            </el-button>
                            <el-button size="mini" type="primary" @click="modifyMenu(scope.row)">修改
                            </el-button>
                            <el-button size="mini" type="success" @click="addSecondMenu(scope.row)"
                                       v-if="scope.row.level==0">添加二级菜单
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>
        </el-row>
    </div>

</template>

<script>
    import {addMenu, getMenuList, updateMenu, deleteMenu} from "@/api/menuApi";
    import CommonUtils from "../../utils/CommonUtils";

    export default {
        name: "sys-menu",
        data() {
            return {
                addForm: {
                    visible: false,
                    id: null,
                    name: null,
                    level: 0,
                    icon: null,
                    url: null,
                    remark: null,
                    parentId: null
                },
                searchParam: {
                    name: null
                },
                tableData: [],
                rules: {
                    name: [{required: true, message: '请输入菜单名称', trigger: 'blur'}],
                    url: [{required: true, message: '请输入菜单路由', trigger: 'blur'}]
                }
            }
        },
        created() {
            this.searchMenuList();
        },
        methods: {
            //显示添加菜单dialog
            showAddMenuDialog() {
                this.addForm = CommonUtils.clearObjectValue(this.addForm);
                this.addForm.level = 0;
                this.addForm.visible = true;
            },
            //查询菜单
            searchMenuList() {
                getMenuList(this.searchParam).then(res => {
                    this.tableData = res;
                })

            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            //新增
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.addForm.id ? this.updateOneMenu() : this.addOneMenu();

                    } else {
                        this.$message.warning("请将信息填写完整");
                    }
                });
            },
            //新增菜单
            addOneMenu() {
                addMenu(this.addForm).then(res => {
                    this.$message.success('新增成功');
                    this.addForm.visible = false;
                    this.searchMenuList();
                })
            },
            //更新菜单
            updateOneMenu() {
                updateMenu(this.addForm).then(res => {
                    this.$message.success('修改成功');
                    this.addForm.visible = false;
                    this.searchMenuList();
                })
            },
            //修改菜单
            modifyMenu(row) {
                this.addForm = CommonUtils.setValueFromOtherObjectByKey(this.addForm, row);
                this.addForm.visible = true;

            },
            //删除
            deletMenuAndChildren(row) {
                this.$confirm('菜单删除后将无法恢复，是否确定删除？', '重要提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let param = {id: row.id}
                    deleteMenu(param).then(res => {
                        this.$message.success("删除成功");
                        this.searchMenuList();
                    }, err => {
                    })
                }).catch(() => {
                })
            },
            //新增二级菜单
            addSecondMenu(row) {
                this.addForm = CommonUtils.clearObjectValue(this.addForm);
                this.addForm.parentId = row.id;//二级菜单parent_id为一级菜单id
                this.addForm.level = 1;//二级菜单
                this.addForm.visible = true;
            }
        }
    }
</script>

<style scoped>
    .menu {

    }

</style>